#include "head.h"


//class Square:public QWidget
//{
//    Q_OBJECT
//public:
//    Square(QWidget *parent = nullptr);
//   ~Square();

//private:
//    void paintEvent(QPaintEvent *event) override;
//    void mouseMoveEvent(QMouseEvent *event) override;
//    void mousePressEvent(QMouseEvent *event) override;
//    void mouseReleaseEvent(QMouseEvent *event) override;
//};


Square::Square(QWidget *parent)
    :QWidget(parent)
{
//    setGeometry(0, 0, Width, Height);
    setMinimumSize(Width, Height);

    setAttribute(Qt::WA_TranslucentBackground, true);
//    setStyleSheet("QLabel {background-color: white;}");//设置样式
//    setAutoFillBackground(true);//执行填充
}

Square::~Square()
{

}


void Square::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        _lastMousePos = event->globalPos();
    }
}

void Square::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        auto diffx = event->globalPos().x() - _lastMousePos.x();
        _lastMousePos = event->globalPos();
//        qDebug() << diffx << "mouseMOve";
        emit sendVolumeControlVolumeLevel(diffx);
    }
}

void Square::mouseReleaseEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
}

void Square::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)

    QPainter paint(this);
    paint.setRenderHints(QPainter::SmoothPixmapTransform|QPainter::Antialiasing, true);
    paint.save();
    QPixmap image(":/images/Resources/volumeButton.png");
    paint.drawPixmap(rect(), image, image.rect());
    paint.restore();

}




VolumeControl::VolumeControl(QWidget *parent)
    :QWidget(parent)
{
    resize(Widget::MAINWIDGET_WIDTH, Widget::MAINWIDGET_HEIGHT - 770);
    setMinimumSize(Widget::MAINWIDGET_WIDTH, Widget::MAINWIDGET_HEIGHT - 770);
    setAttribute(Qt::WA_TranslucentBackground, true);
    _volumeSize = 0;
    //这是为了减去开头的圆角矩形长度和包裹音量条的外壳厚度
    _volumeSize = _volumeSize - 14 - 13;//不能放在paintevent中否则会随着操作系统自动触发paintevent，导致_volumeSize自动减少的现象。
    loadLayOut();//有时候不出现，就看看函数是否执行了
//    setStyleSheet("QLabel {background-color: white;}");//设置样式
//    setAutoFillBackground(true);//执行填充
}


void VolumeControl::acceptVolumeLevel(int x)
{
    auto X = _mark->pos().x() + x;
    if(X >= VolumeStartX && X <= VolumeWidth)
    {
        _mark->move(X, _mark->pos().y());
        _volumeSize = X - VolumeStartX;
        _volumeRatio = static_cast<double>(_volumeSize)/static_cast<double>(VolumeWidth - VolumeStartX);
        qDebug() << _volumeRatio << X << "111";
        emit changeVolume(_volumeRatio);
        repaint();
    }
}

void VolumeControl::loadLayOut()
{
    _mark = new Square(this);
    _mark->setGeometry(VolumeStartX, VolumeStartY, Square::Width, Square::Height);
    _mark->setHidden(false);
    connect(_mark, SIGNAL(sendVolumeControlVolumeLevel(int)), this, SLOT(acceptVolumeLevel(int)));
}

void VolumeControl::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)

    QPainter paint(this);
    paint.setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform, true);
    paint.save();

    QPixmap image(":/images/Resources/volumeBackground.png");
    auto aimRect = QRect(VolumeStartX, VolumeStartY, VolumeWidth, VolumeHeight);
    paint.drawPixmap(aimRect, image, image.rect());
    paint.restore();

    paint.save();
    QPixmap image2(":/images/Resources/volumeValue.png");
    auto color = image2.toImage().pixel(10,10);
    paint.setPen(color);
    paint.setBrush(QBrush(color));
    paint.drawRoundedRect(VolumeStartX + 13, VolumeStartY + 18, 14, 14, 2, 2);


    if(_volumeSize <= 0)
    {
        _volumeSize = 0;
    }
    paint.drawRect(QRect(VolumeStartX + 13 + 14, VolumeStartY + 18, _volumeSize, 14));
    paint.restore();

}

VolumeControl::~VolumeControl()
{

}
